package com.wngbms._03_classload;

public class Demo {

    static {
        System.out.println("load Demo.class"); // 初始化主类
    }


    public static void main(String[] args) throws Exception {
        System.out.println("hello world");

//        new C1(); // 实例化目标类型 会加载目标类
//        C1.f1();      // 调用目标类型static方法 会加载目标类,  同时会加载父类
//        int a = C1.a;       // 访问目标类型静态字段 会加载目标类
        C1.a = 100;
//        int b = C1.b;           // 被final修饰, 编译期放入常量池, 不会触发初始化, 也不会加载C1

        try {
            // Class.forName("com.wngbms.classload.C1"); // 加载目标类
            // Class.forName("com.wngbms.classload.C1", true, Demo.class.getClassLoader()); // 加载目标类
            // Class.forName("com.wngbms.classload.C1", false, Demo.class.getClassLoader()); // 不会加载目标类
//            Demo.class.getClassLoader().loadClass("com.wngbms.classload.C1"); //
        } catch (Exception e) {
            //TODO: handle exception
        }

//        TimeUnit.SECONDS.sleep(Integer.MAX_VALUE);
    }
}

/*

D:\Code\_01_jvm>javap -c out\production\_01_jvm\com\wngbms\classload\Demo.class
Compiled from "Demo.java"
public class com.wngbms.classload.Demo {
  public com.wngbms.classload.Demo();
    Code:
       0: aload_0
       1: invokespecial #1                  // Method java/lang/Object."<init>":()V
       4: return

  public static void main(java.lang.String[]) throws java.lang.Exception;
    Code:
       0: getstatic     #2                  // Field java/lang/System.out:Ljava/io/PrintStream;
       3: ldc           #3                  // String hello world
       5: invokevirtual #4                  // Method java/io/PrintStream.println:(Ljava/lang/String;)V
       8: new           #5                  // class com/wngbms/classload/C1
      11: dup
      12: invokespecial #6                  // Method com/wngbms/classload/C1."<init>":()V
      15: pop
      16: invokestatic  #7                  // Method com/wngbms/classload/C1.f1:()V
      19: getstatic     #8                  // Field com/wngbms/classload/C1.a:I
      22: istore_1
      23: bipush        100
      25: putstatic     #8                  // Field com/wngbms/classload/C1.a:I
      28: ldc           #9                  // class com/wngbms/classload/Demo
      30: invokevirtual #10                 // Method java/lang/Class.getClassLoader:()Ljava/lang/ClassLoader;
      33: ldc           #11                 // String com.wngbms.classload.C1
      35: invokevirtual #12                 // Method java/lang/ClassLoader.loadClass:(Ljava/lang/String;)Ljava/lang/Class;
      38: pop
      39: goto          43
      42: astore_2
      43: return
    Exception table:
       from    to  target type
          28    39    42   Class java/lang/Exception

  static {};
    Code:
       0: getstatic     #2                  // Field java/lang/System.out:Ljava/io/PrintStream;
       3: ldc           #14                 // String load Demo.class
       5: invokevirtual #4                  // Method java/io/PrintStream.println:(Ljava/lang/String;)V
       8: return
}



*/